//
//  UFONovelViewController.m
//  unrivalledForUniverse
//
//  Created by dllo on 16/1/21.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "UFONovelViewController.h"
#import "UFONovelCollectionViewCell.h"
#import "UFONovelModel.h"
#import "LQCAFNetTool.h"
#import "UFOEpisodesViewController.h"
@interface UFONovelViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic, retain) UICollectionView *collectionV;
@property (nonatomic, retain) UICollectionViewFlowLayout *flowL;
@property (nonatomic, retain) NSMutableArray *novelArr;
@end

@implementation UFONovelViewController
- (void)dealloc
{
    
    self.collectionV.delegate = nil;
    self.collectionV.dataSource = nil;
    [_collectionV release];
    [_flowL release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self createCollectionView];
    [self getData];
}

#pragma 创建collectionView
- (void)createCollectionView
{
    self.view.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(135 * SCREENWIDTH / WIDTHSIXP, 30 * SCREENHEIGHT / HEIGHTSIXP, 100 * SCREENWIDTH / WIDTHSIXP, 50 * SCREENHEIGHT / HEIGHTSIXP)];
    label.text = @"我的小说";
    label.font = [UIFont systemFontOfSize:20.0];
    label.textColor = [UIColor purpleColor];
    label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:label];
    [label release];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(0 * SCREENWIDTH / WIDTHSIXP, 30 * SCREENHEIGHT / HEIGHTSIXP, 50 * SCREENWIDTH / WIDTHSIXP, 50 * SCREENHEIGHT / HEIGHTSIXP);
    [button setImage:[UIImage imageNamed:@"fanhui232"] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    
    self.flowL = [[[UICollectionViewFlowLayout alloc] init] autorelease];
    
    self.flowL.minimumLineSpacing = 50 * SCREENWIDTH / WIDTHSIXP;
    self.flowL.headerReferenceSize = CGSizeMake(10 * SCREENWIDTH / WIDTHSIXP, 10 * SCREENHEIGHT / HEIGHTSIXP);
    _flowL.itemSize = CGSizeMake(170 * SCREENWIDTH / WIDTHSIXP, 220 * SCREENHEIGHT / HEIGHTSIXP);
    self.collectionV.frame = CGRectMake(0, 0, SCREENWIDTH, 950 * SCREENHEIGHT / HEIGHTSIXP);
    self.flowL.sectionInset = UIEdgeInsetsMake(0, 10 * SCREENWIDTH / WIDTHSIXP, 0, 10 * SCREENWIDTH / WIDTHSIXP);
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 100 * SCREENHEIGHT / HEIGHTSIXP, SCREENWIDTH, SCREENHEIGHT) collectionViewLayout:self.flowL];
    self.collectionV.backgroundColor = [UIColor whiteColor];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.view addSubview:self.collectionV];
    [self.collectionV release];
    [self.collectionV registerClass:[UFONovelCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}

- (void)getData
{
    
    self.novelArr = [NSMutableArray array];
    NSString *url =  @"http://lol.zhangyoubao.com/apis/rest/CatalogsService/all?cattype=novel&i_=801844CE-87DA-42D5-B028-286599ACDF87&t_=1453341294&p_=6227&v_=40070204&d_=ios&osv_=9.2&version=0&a_=lol";
    
    [LQCAFNetTool getNetWithURL:url body:nil headFile:nil responseStyle:LQCJSON success:^(NSURLSessionDataTask *task, id responseObject) {
        
        NSMutableDictionary *dic = responseObject;
        NSMutableArray *arr1 = [dic objectForKey:@"data"];
        NSMutableDictionary *tempDic = [arr1 objectAtIndex:0];
        NSMutableArray *arr = [tempDic objectForKey:@"catword_id"];
        self.novelArr = [UFONovelModel baseModelWithArr:arr];
        NSLog(@"%@",self.novelArr);
        [self.collectionV reloadData];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
    }];

}

- (void)buttonAction
{
    
    [self dismissViewControllerAnimated:YES completion:^{
        
        
    }];
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    UFOEpisodesViewController *episodesVC = [[UFOEpisodesViewController alloc] init];
    UFONovelModel *model = [self.novelArr objectAtIndex:indexPath.row];
    episodesVC.tagL = model.mId;
    episodesVC.storyImageV = model.pic_url;
    episodesVC.storyName = model.name;
    episodesVC.storyIntro = model.desc;
    [self presentViewController:episodesVC animated:YES completion:^{
        
        
    }];
    
    
}

// cell个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.novelArr.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    UFONovelCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    UFONovelModel *model = [self.novelArr objectAtIndex:indexPath.row];
    [cell.titleImage sd_setImageWithURL:[NSURL URLWithString:model.pic_url] placeholderImage:nil];
    cell.signname.text = model.name;
    return cell;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
